/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * test.java
 *
 * Created on 09/04/2011, 15:28:15
 */

package Business;

import objects.Frm;
import Domain.DataManager;
import Domain.UserClient.ButtonsFunctions;
import GUI.ChooseForum;
import GUI.ViewNotifications;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


/**
 *
 * @author Rubik Levitsky
 */
public class ToolbarFrame extends javax.swing.JFrame implements FrameType
{

     private JFrame _currentFrame;
     Vector<String> _notifications;
     final Object notificationsLock;
      Thread notificationReader;
//     private JSplitPane jSplitPane1;

    public ToolbarFrame()
    {
        initComponents();
        this._notifications = new Vector<String>();
        this.notificationsLock = new Object();

        notifications.setText(this._notifications.size() + " Notifications");

        this.notificationReader = new Thread(new Runnable() {
                @Override
                public void run()
                {
                    while(true)
                    {
                            String notif = DataManager.getDataManager().getNotification();
                            synchronized(notificationsLock)
                            {
                                _notifications.add(notif);
                                notifications.setText(_notifications.size() + " Notifications");
                            }
                    }
                }
            });
            this.notificationReader.start();
    }

    @Override
    public void initiateFrame()
    {
        _currentFrame = new JFrame();
        this._notifications = new Vector<String>();
        notifications.setText(_notifications.size() + " Notifications");
    }

    @Override
    public void initiateFrame(Object obj, int type)
    {
        this._notifications = new Vector<String>();
        notifications.setText(_notifications.size() + " Notifications");
        _currentFrame = new ChooseForum((Vector<Frm>)obj, type);
        _currentFrame.add(logout);
        _currentFrame.add(notifications);
        userName.setText("Hello " + DataManager.getDataManager().getCurrUser());
        _currentFrame.add(userName);

        _currentFrame.setVisible(true);
    }

    @Override
    public void dispose()
    {
        _currentFrame.dispose();
    }

    @Override
    public void changeCurrentFrame(JFrame newFrame)
    {
        _currentFrame.dispose();
        _currentFrame = newFrame;
        _currentFrame.add(jPanel1);

        
        Toolkit tk = Toolkit.getDefaultToolkit();
        //Dimension screenSize = tk.getScreenSize();
//        Dimension screenSize = _currentFrame.getSize();
//        int screenHeight = screenSize.height;
//        int screenWidth = screenSize.width;
//        jPanel1.setLocation(screenWidth , 0);

        //_currentFrame.add(logout);
       // _currentFrame.add(notifications);
        userName.setText("Hello " + DataManager.getDataManager().getCurrUser());
        //_currentFrame.add(userName);
        _currentFrame.setVisible(true);
    }

    @Override
    public synchronized void showErrorMessage(String error)
    {
        JOptionPane.showMessageDialog(_currentFrame.getContentPane(), error, "Error", JOptionPane.ERROR_MESSAGE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        logout = new javax.swing.JButton();
        userName = new javax.swing.JLabel();
        notifications = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setEnabled(false);

        logout.setText("Logout");
        logout.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutActionPerformed(evt);
            }
        });

        userName.setForeground(new java.awt.Color(153, 0, 153));
        userName.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        userName.setText("Hello ");

        notifications.setText("Notifications");
        notifications.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        notifications.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                notificationsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(notifications, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(userName, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(logout))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(logout)
                    .addComponent(userName)
                    .addComponent(notifications)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(135, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(266, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void logoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutActionPerformed
        ButtonsFunctions.getButtonsFunctions().processDisconnect();
        FrameHandler.getFrameHandler().changeToOtherTypeOfFrame(FrameHandler.NOTOOLBAR);//opens defualt NOTOOLBAR frame, which is the Welcome Frame
    }//GEN-LAST:event_logoutActionPerformed

    private void notificationsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_notificationsActionPerformed
        ViewNotifications notifs;
        synchronized(notificationsLock)
        {
            notifs = new ViewNotifications(this._notifications);
            this._notifications = new Vector<String>();
            notifications.setText(this._notifications.size() + " Notifications");
        }
        notifs.setVisible(true);
    }//GEN-LAST:event_notificationsActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton logout;
    private javax.swing.JButton notifications;
    private javax.swing.JLabel userName;
    // End of variables declaration//GEN-END:variables

    @Override
    public void showInfoMessage(String info) {
        JOptionPane.showMessageDialog(_currentFrame.getContentPane(), info, "INFO", JOptionPane.INFORMATION_MESSAGE);
    }


}
